﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace CAN_Monitor
{
    public partial class frmConfig : Form
    {
        #region ctor

        public frmConfig()
        {
            InitializeComponent();
        }

        #endregion

        #region Members

        private SerialPort mPort = null;

        #endregion

        #region Propteries

        /// <summary>
        /// Gets the configured port
        /// </summary>
        public SerialPort Port
        {
            get
            {
                return this.mPort;
            }
        }

        #endregion

        #region Methods



        #endregion

        #region Events

        private void btnOk_Click(object sender, EventArgs e)
        {
            Parity p = Parity.None;
            switch (this.cmbParity.SelectedIndex)
            {
                case 0:
                    p = Parity.Even;
                    break;
                case 1:
                    p = Parity.Odd;
                    break;
                case 2:
                    p = Parity.None;
                    break;
            }

            mPort = new SerialPort(cmbPort.SelectedItem.ToString(), 
                int.Parse(this.cmbBaudrate.SelectedItem.ToString()), p, 
                int.Parse(this.cmbDatabit.SelectedItem.ToString()[0].ToString()));

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void frmConfig_Load(object sender, EventArgs e)
        {
            this.cmbPort.Items.AddRange(SerialPort.GetPortNames());

            this.cmbDatabit.SelectedIndex = 0;
            this.cmbStopbit.SelectedIndex = 0;
            this.cmbParity.SelectedIndex = 1;
            this.cmbPort.SelectedIndex = 0;
            this.cmbParity.SelectedIndex = 2;
            this.cmbBaudrate.SelectedIndex = 3;
        }

        #endregion
    }
}